cmake_minimum_required(VERSION 3.5)
# 设置默认构建类型为 Release，但允许切换到 Debug
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build" FORCE)
endif()

# 设置可用的构建类型
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release")
# 指定交叉编译工具/桌面编译器
# set(Qt5_DIR "/home/syl/qt_lib_aarch64-linux-gnu_6.5/qt-everywhere-src-5.15.2/lib/cmake/Qt5")
# set(COMPILER_PREFIX "/home/syl/qt_need/compiler/gcc-linaro-6.5.0-2018.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu")
# set(CMAKE_C_COMPILER ${COMPILER_PREFIX}-gcc)
# set(CMAKE_CXX_COMPILER ${COMPILER_PREFIX}-g++)

project(max_module VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置输出目录
set(OUTPUT_DIR "${CMAKE_BINARY_DIR}/../output/bin/")
file(MAKE_DIRECTORY ${OUTPUT_DIR})

# 恢复被注释的Qt查找逻辑
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
# 集中定义所有源文件
set(PROJECT_SOURCES
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    pluginmanager/IPlugin.h
    pluginmanager/pluginmanager.cpp
    pluginmanager/pluginmanager.h
    maxtool/max_stackwidget.h
    maxtool/max_stackwidget.cpp
    maxtool/DateTimeButton/DateTimeButton.h
    maxtool/DateTimeButton/DateTimeButton.cpp
    maxtool/LineEdit/customlineedit.h
    maxtool/LineEdit/customlineedit.cpp
    README.md
    # output/config/config.json
)

# if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
#     qt_add_executable(max_module
#         MANUAL_FINALIZATION
#         ${PROJECT_SOURCES}
#     )
# else()
    add_executable(max_module
        ${PROJECT_SOURCES}
        displaywidget.h displaywidget.cpp displaywidget.ui

        logindialog.h logindialog.cpp logindialog.ui

    )
# endif()

# 设置目标属性
set_target_properties(max_module PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}"
    MACOSX_BUNDLE_OUTPUT_DIRECTORY "${OUTPUT_DIR}"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}"
)

# 添加头文件目录（必须在目标创建后）
target_include_directories(max_module PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/maxtool
    ${CMAKE_CURRENT_SOURCE_DIR}/pluginmanager
)


target_link_libraries(max_module PRIVATE
    Qt${QT_VERSION_MAJOR}::Widgets
    # Qt${QT_VERSION_MAJOR}::Sql  # 新增此行
)
# 其他设置（如 macOS Bundle 配置）
# if(${QT_VERSION} VERSION_LESS 6.1.0)
#   set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.max_module)
# endif()
set_target_properties(max_module PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS max_module
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(max_module)
endif()

add_subdirectory(pluginmanager/plugin/plugin_config)
add_subdirectory(pluginmanager/plugin/plugin_warn)
add_subdirectory(pluginmanager/plugin/plugin_output)
add_subdirectory(pluginmanager/plugin/plugin_database)
add_subdirectory(pluginmanager/plugin/plugin_datablock)
add_subdirectory(pluginmanager/plugin/plugin_eventRecord)
add_subdirectory(pluginmanager/plugin/plugin_modbus)
